package com.pickgliss.bitmapEngin.ds
{
	import com.pickgliss.bitmapEngin.cache.CacheClip;

	public class CacheClipPool extends CacheClipList
	{
		public function CacheClipPool(size:int = 2,growthRate:int = 1)
		{
			super();
			_size = size;
			_growthRate = growthRate;
			_freeList = new CacheClipList();
			increaceSize(_size);
		}

		protected var _freeList:CacheClipList;
		protected var _growthRate:int;
		protected var _size:int;
		
		public function create(addToTail:Boolean ) : CacheClip
		{
			if (!_freeList.head)increaceSize(_growthRate);
			var clip:CacheClip = _freeList.removeHead();
			if (addToTail)
			{
				append(clip);
			}
			else
			{
				prepend(clip);
			}
			return clip;
		}
		
		override public function dispose():void
		{
			super.dispose();
			_freeList.dispose();
			_freeList = null;
		}
		
		public function free(clip:CacheClip) : void
		{
			clip.z = NaN;
			clip.visible = true;
			clip.isPlaying = true;
			clip.colorTransform = null;
			_freeList.append(remove(clip));
		}
		
		public function increaceSize(increace:int):void
		{
			for(var i:int = 0;i < increace;i++)
			{
				_freeList.append(new CacheClip(null));
			}
		}
		
		public function get size():int
		{
			return _size;
		}
		
		override public function toString():String
		{
			var output:String;
			output = super.toString() ;
			output += "\nfreelist : \n" + _freeList.toString();
			return output;
		}
	}
}